package cn.utils;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

import cn.entity.User;

public class JDBCUtil {

	static Properties pros = null;
	static {
		pros = new Properties();
		try {
			pros.load(Thread.currentThread().getContextClassLoader()
					.getResourceAsStream("db.properties"));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static Connection getConnection() {

		try {
			Class.forName(pros.getProperty("jdbcDriver"));
			return DriverManager.getConnection(pros.getProperty("jdbcUrl"),
					pros.getProperty("jdbcUser"), pros.getProperty("jdbcPwd"));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;

	}

	public static void close(ResultSet rs, PreparedStatement ps, Connection conn) {

		if (rs != null) {
			try {
				rs.close();
			} catch (SQLException e) {

				e.printStackTrace();
			}
		}
		if (ps != null) {
			try {
				ps.close();
			} catch (SQLException e) {

				e.printStackTrace();
			}
		}

		if (conn != null) {
			try {
				conn.close();
			} catch (SQLException e) {

				e.printStackTrace();
			}
		}

	}

	public static void release(Connection connection) {
		try {
			if (connection != null) {
				connection.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
			try {
				throw new SQLException("数据库连接出错！");
			} catch (SQLException e1) {

				e1.printStackTrace();
			}
		}
	}

	public static void release(ResultSet rs, Statement statement) {
		try {
			if (rs != null) {
				rs.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
			try {
				throw new SQLException("数据库连接出错！");
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}

		try {
			if (statement != null) {
				statement.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
			try {
				throw new SQLException("数据库连接出错！");
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
	}

	// 添加记录

	public static void add(User user, String sql) {

		Connection conn = getConnection();
		try {
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setString(1, user.getUsername());
			ps.setString(2, user.getPassword());
			ps.executeUpdate();
		} catch (SQLException e) {

			e.printStackTrace();
		}

	}


	public static void insert(Connection conn, String sql) {

		Connection cons = getConnection();
		sql = "insert insert users(username,password) values(?,?)";
		try {
			PreparedStatement ps = cons.prepareStatement(sql);
			ps.executeUpdate();

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public static User getByUsername(String username) {
		User user = new User();
		Connection con = getConnection();
		PreparedStatement pst = null;
		String str = "select username,password from t_user where username =?";
		try {
			pst = con.prepareStatement(str);
			pst.setString(1, username);
			ResultSet rs = null;
			rs = pst.executeQuery();
			while (rs.next()) {
				user.setUsername(rs.getString(1));
				user.setPassword(rs.getString(2));
			}
			rs.close();
			pst.close();
			con.close();
		} catch (Exception e) {
			// TODO: handle exception
		}
		System.out.println(user.getUsername()+","+user.getPassword());
		return user;
	}

}
